﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace TimKen.DataAccess
{
	/// <summary>
	/// 数据访问类ShipDAO。
	/// </summary>
	public class ShipDAO
	{
		public ShipDAO()
		{}
		#region  成员方法

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string ShipNo)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
			parameters[0].Value = ShipNo;

			int result= DbHelperSQL.RunProcedure("UP_Ship_Exists",parameters,out rowsAffected);
			if(result==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  增加一条数据
		/// </summary>
		public void Add(TimKen.DataEntity.Ship model)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,50),
					new SqlParameter("@ShipDate", SqlDbType.DateTime),
					new SqlParameter("@Remark", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.ShipNo;
			parameters[1].Value = model.CardCode;
			parameters[2].Value = model.ShipDate;
			parameters[3].Value = model.Remark;

			DbHelperSQL.RunProcedure("UP_Ship_ADD",parameters,out rowsAffected);
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public void Update(TimKen.DataEntity.Ship model)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,50),
					new SqlParameter("@ShipDate", SqlDbType.DateTime),
					new SqlParameter("@Remark", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.ShipNo;
			parameters[1].Value = model.CardCode;
			parameters[2].Value = model.ShipDate;
			parameters[3].Value = model.Remark;

			DbHelperSQL.RunProcedure("UP_Ship_Update",parameters,out rowsAffected);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public void Delete(string ShipNo)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
			parameters[0].Value = ShipNo;

			DbHelperSQL.RunProcedure("UP_Ship_Delete",parameters,out rowsAffected);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public TimKen.DataEntity.Ship GetModel(string ShipNo)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
			parameters[0].Value = ShipNo;

			TimKen.DataEntity.Ship model=new TimKen.DataEntity.Ship();
			DataSet ds= DbHelperSQL.RunProcedure("UP_Ship_GetModel",parameters,"ds");
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ShipNo"].ToString()!="")
				{
                    model.ShipNo = (string)ds.Tables[0].Rows[0]["ShipNo"];
				}
				model.CardCode=ds.Tables[0].Rows[0]["CardCode"].ToString();
				if(ds.Tables[0].Rows[0]["ShipDate"].ToString()!="")
				{
					model.ShipDate=DateTime.Parse(ds.Tables[0].Rows[0]["ShipDate"].ToString());
				}
				model.Remark=ds.Tables[0].Rows[0]["Remark"].ToString();
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ShipNo,CardCode,ShipDate,Remark ");
			strSql.Append(" FROM Ship ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Ship";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  成员方法
	}
}

